####################ISSUE SALIENCE EFFECTS ############
library(lme4)
library(texreg)
library(sjPlot)
library(sjstats)
library(fixest)
library(ggplot2)
library(ggeffects)
library(zoo)
library(skimr)
library(tidyverse)
load("Data/EP_debates_07062023.Rdata")

##### LOAD DATA FROM RAUH AND DE WILDE

load("Data/RdW_salience.Rdata")

### PREP RdW Data for Merging
padpanel$eu.w[is.na(padpanel$eu.w)] <- 0
months_sal <- padpanel %>% group_by(country, month) %>% summarise(EU_sal= mean(eu.w))

months_sal$year_mon <- as.yearmon(months_sal$month)
months_sal <- mutate(months_sal,
                      country = case_when(country == "DE" ~ "Germany",
                                          country == "ES" ~ "Spain",
                                          country == "NL" ~ "Netherlands",
                                          country == "UK" ~ "United Kingdom"))
months_sal$month <- NULL

EP_debates$year_mon <- as.yearmon(EP_debates$date)

EP_debates_sal <- left_join(EP_debates, months_sal, by = c("country", "year_mon"))



EP_debates_sal$radical <- abs(EP_debates_sal$lrgen - 5)
range(EP_debates_sal$radical, na.rm = TRUE)

centFUN <- function(x) {
  x - mean(x, na.rm = TRUE)
}

result <- apply(EP_debates_sal[,c(22:24, 27:71, 73:75, 77:107,117:118)], MARGIN = 2, FUN = centFUN)
result <- as.data.frame(result)


EP_eurobar_cent <- cbind(EP_debates_sal[,-c(22:24, 27:71, 73:75, 77:107,117:118)], result)

rm(list=setdiff(ls(), "EP_eurobar_cent"))

EP_eurobar_cent$embed_dict_scale <- scale(EP_eurobar_cent$embed_dict)
EP_eurobar_cent$aut_dict_scale <- scale(EP_eurobar_cent$aut_dict)
EP_eurobar_cent$immi_dict_scale <- scale(EP_eurobar_cent$immi_dict)
EP_eurobar_cent$int_dict_scale <- scale(EP_eurobar_cent$int_dict)
EP_eurobar_cent$lrgen_scale <- scale(EP_eurobar_cent$lrgen)
EP_eurobar_cent$radical_scale <- scale(EP_eurobar_cent$radical)

EP_eurobar_cent$aut_sal <- scale(EP_eurobar_cent$mip_n_govdebt_ipol)
EP_eurobar_cent$immi_sal <- scale(EP_eurobar_cent$mip_n_immigration_ipol)
EP_eurobar_cent$image_ipol <- scale(EP_eurobar_cent$image_ipol)
EP_eurobar_cent$EU_sal <- scale(EP_eurobar_cent$EU_sal)

##### AUSTERITY SALIENCE (EUROBAROMETER)

AUT_sal_model <- lmer(aut_dict_scale ~ challenge*aut_sal + lrgen_scale  + EP_Coa + nat_opp +de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AUT_sal_model)


##### IMMIGRATION SALIENCE (EUROBAROEMTER)


Immi_sal_model <- lmer(immi_dict_scale ~ challenge*immi_sal + lrgen_scale  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                         de_agri + de_labour + de_edu + de_envi + de_energy + 
                         de_immi + de_transport + de_law + de_welfare  + de_commerce +
                         de_defense + de_techno + de_trade + de_intern + de_govern +
                         de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                         (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Immi_sal_model)

##### EU-SALIENCE (RdW)

EU_sal_model <- lmer(int_dict_scale ~ challenge*EU_sal + EP_Coa + lrgen_scale  + nat_opp + de_macro + de_civil + de_health + 
                       de_agri + de_labour + de_edu + de_envi + de_energy + 
                       de_immi + de_transport + de_law + de_welfare  + de_commerce +
                       de_defense + de_techno + de_trade + de_intern + de_govern +
                       de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + as.factor(year) + as.factor(country) +
                       (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(EU_sal_model)

##### combine them in one table


texreg(list(AUT_sal_model, Immi_sal_model, EU_sal_model),
       file = "Tables/issues_sal_08062023.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "aut_sal" = "Austerity Salience",
                              "immi_sal" = "Immigration Salience",
                              "EU_sal" = "EU Salience",
                              "challenge:aut_sal" = "Challenger * Austerity Salience",
                              "challenge:immi_sal" = "Challenger * Immigration Salience",
                              "challenge:EU_sal" = "Challenger * EU Salience",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes")),
       custom.model.names = c("Austerity", "Immigration", "EU-Integration"),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       caption = "The effect of changes in issue salience on issue emphasis")


###### ALTERNATIVE MODEL SPECIFICATIONS DEBATE INTERCEPTS #######

AUT_sal_DR <- lmer(aut_dict_scale ~ challenge*aut_sal + lrgen_scale  + EP_Coa + as.factor(year) + as.factor(country) +
                        (1|debate) + (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))


Immi_sal_DR <- lmer(immi_dict_scale ~ challenge*immi_sal + lrgen_scale  + EP_Coa + as.factor(year) + as.factor(country) +
                     (1|debate) + (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))


Int_sal_DR <- lmer(int_dict_scale ~ challenge*EU_sal + lrgen_scale  + EP_Coa + as.factor(year) + as.factor(country) +
                      (1|debate) + (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(list(AUT_sal_DR, Immi_sal_DR, Int_sal_DR),
       file = "Tables/issues_sal_altspecs_080623.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "aut_sal" = "Austerity Salience",
                              "immi_sal" = "Immigration Salience",
                              "EU_sal" = "EU Salience",
                              "challenge:aut_sal" = "Challenger * Austerity Salience",
                              "challenge:immi_sal" = "Challenger * Immigration Salience",
                              "challenge:EU_sal" = "Challenger * EU Salience",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes")),
       custom.model.names = c("Austerity", "Immigration", "EU-Integration"),
       custom.note = "%stars. Random Intercepts for Parties, MEPs and Debates",
       digits = 2,
       caption = "The effect of changes in issue salience on issue emphasis")


##### ALT SPECS OLS #####


AUT_sal_FE <- feols(aut_dict_scale ~ challenge*aut_sal + lrgen_scale   + EP_Coa + nat_opp+ lrgen_scale  + de_macro + de_civil + de_health + 
                     de_agri + de_labour + de_edu + de_envi + de_energy + 
                     de_immi + de_transport + de_law + de_welfare  + de_commerce +
                     de_defense + de_techno + de_trade + de_intern + de_govern +
                     de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5| year + country, data = EP_eurobar_cent, cluster = "party_id")

Immi_sal_FE <- feols(immi_dict_scale ~ challenge*immi_sal + lrgen_scale   + EP_Coa + nat_opp+  lrgen_scale  + de_macro + de_civil + de_health + 
                     de_agri + de_labour + de_edu + de_envi + de_energy + 
                     de_immi + de_transport + de_law + de_welfare  + de_commerce +
                     de_defense + de_techno + de_trade + de_intern + de_govern +
                     de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5| year + country, data = EP_eurobar_cent, cluster = "party_id")

Int_sal_FE <- feols(int_dict_scale ~ challenge*EU_sal + lrgen_scale   + EP_Coa +  nat_opp+ lrgen_scale  + de_macro + de_civil + de_health + 
                     de_agri + de_labour + de_edu + de_envi + de_energy + 
                     de_immi + de_transport + de_law + de_welfare  + de_commerce +
                     de_defense + de_techno + de_trade + de_intern + de_govern +
                     de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5| year + country, data = EP_eurobar_cent, cluster = "party_id")

texreg(list(AUT_sal_FE, Immi_sal_FE, Int_sal_FE),
       file = "Tables/issues_sal_OLS_080623.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "aut_sal" = "Austerity Salience",
                              "immi_sal" = "Immigration Salience",
                              "EU_sal" = "EU Salience",
                              "challenge:aut_sal" = "Challenger * Austerity Salience",
                              "challenge:immi_sal" = "Challenger * Immigration Salience",
                              "challenge:EU_sal" = "Challenger * EU Salience",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes")),
       custom.model.names = c("Austerity", "Immigration", "EU-Integration"),
       custom.note = "%stars. Clustered Standard Errors (National Party) in Parentheses",
       digits = 2,
       caption = "The effect of changes in issue salience on issue emphasis")


##### CHECKED AND RUN ON 08.06.2023

### Update in response to JEPP REVIEWER COMMENTS
EP_eurobar_left <- subset(EP_eurobar_cent, EP_eurobar_cent$lrgen < 0)

EP_eurobar_right <- subset(EP_eurobar_cent, EP_eurobar_cent$lrgen > 0)


##### AUSTERITY SALIENCE (EUROBAROMETER)

AUT_sal_model_left <- lmer(aut_dict_scale ~ challenge*aut_sal + lrgen_scale  + EP_Coa + nat_opp +de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_left, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AUT_sal_model_left)

AUT_sal_model_right<- lmer(aut_dict_scale ~ challenge*aut_sal + lrgen_scale  + EP_Coa + nat_opp +de_macro + de_civil + de_health + 
                             de_agri + de_labour + de_edu + de_envi + de_energy + 
                             de_immi + de_transport + de_law + de_welfare  + de_commerce +
                             de_defense + de_techno + de_trade + de_intern + de_govern +
                             de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                             (1|mep_ids) + (1|party_id), data = EP_eurobar_right, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AUT_sal_model_right)


##### IMMIGRATION SALIENCE (EUROBAROEMTER)


Immi_sal_model_left <- lmer(immi_dict_scale ~ challenge*immi_sal + lrgen_scale  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                         de_agri + de_labour + de_edu + de_envi + de_energy + 
                         de_immi + de_transport + de_law + de_welfare  + de_commerce +
                         de_defense + de_techno + de_trade + de_intern + de_govern +
                         de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                         (1|mep_ids) + (1|party_id), data = EP_eurobar_left, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Immi_sal_model_left)

Immi_sal_model_right <- lmer(immi_dict_scale ~ challenge*immi_sal + lrgen_scale  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                              de_agri + de_labour + de_edu + de_envi + de_energy + 
                              de_immi + de_transport + de_law + de_welfare  + de_commerce +
                              de_defense + de_techno + de_trade + de_intern + de_govern +
                              de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                              (1|mep_ids) + (1|party_id), data = EP_eurobar_right, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Immi_sal_model_right)


##### EU-SALIENCE (RdW)

EU_sal_model_left <- lmer(int_dict_scale ~ challenge*EU_sal + EP_Coa + lrgen_scale  + nat_opp + de_macro + de_civil + de_health + 
                       de_agri + de_labour + de_edu + de_envi + de_energy + 
                       de_immi + de_transport + de_law + de_welfare  + de_commerce +
                       de_defense + de_techno + de_trade + de_intern + de_govern +
                       de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + as.factor(year) + as.factor(country) +
                       (1|mep_ids) + (1|party_id), data = EP_eurobar_left, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(EU_sal_model_left)

EU_sal_model_right <- lmer(int_dict_scale ~ challenge*EU_sal + EP_Coa + lrgen_scale  + nat_opp + de_macro + de_civil + de_health + 
                            de_agri + de_labour + de_edu + de_envi + de_energy + 
                            de_immi + de_transport + de_law + de_welfare  + de_commerce +
                            de_defense + de_techno + de_trade + de_intern + de_govern +
                            de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + as.factor(year) + as.factor(country) +
                            (1|mep_ids) + (1|party_id), data = EP_eurobar_right, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(EU_sal_model_right)



##### combine them in one table

texreg(list(AUT_sal_model_left, Immi_sal_model_left, EU_sal_model_left),
       file = "Tables/issues_sal_left_120224.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "aut_sal" = "Austerity Salience",
                              "immi_sal" = "Immigration Salience",
                              "EU_sal" = "EU Salience",
                              "challenge:aut_sal" = "Challenger * Austerity Salience",
                              "challenge:immi_sal" = "Challenger * Immigration Salience",
                              "challenge:EU_sal" = "Challenger * EU Salience",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes")),
       custom.model.names = c("Austerity", "Immigration", "EU-Integration"),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       caption = "The effect of changes in issue salience on issue emphasis among left parties")



texreg(list(AUT_sal_model_right, Immi_sal_model_right, EU_sal_model_right),
       file = "Tables/issues_sal_right_120224.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "aut_sal" = "Austerity Salience",
                              "immi_sal" = "Immigration Salience",
                              "EU_sal" = "EU Salience",
                              "challenge:aut_sal" = "Challenger * Austerity Salience",
                              "challenge:immi_sal" = "Challenger * Immigration Salience",
                              "challenge:EU_sal" = "Challenger * EU Salience",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes")),
       custom.model.names = c("Austerity", "Immigration", "EU-Integration"),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       caption = "The effect of changes in issue salience on issue emphasis among right parties")




########### COMBINE CYCLE AND SALIENCE IN RESPONSE TO REVIEWER 2 #################
EP_eurobar_cent$days_from_EP_scale <- scale(EP_eurobar_cent$days_from_EP)
EP_eurobar_cent$days_from_nat_scale <- scale(EP_eurobar_cent$days_from_nat)

AUT_sal_cycle <- lmer(aut_dict_scale ~ challenge*aut_sal*days_from_nat_scale + challenge*aut_sal*days_from_EP_scale + lrgen_scale  + EP_Coa + nat_opp +de_macro + de_civil + de_health + 
                             de_agri + de_labour + de_edu + de_envi + de_energy + 
                             de_immi + de_transport + de_law + de_welfare  + de_commerce +
                             de_defense + de_techno + de_trade + de_intern + de_govern +
                             de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                             (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AUT_sal_cycle)


AUT_sal_cycle_DR <- lmer(aut_dict_scale ~ challenge*aut_sal*days_from_nat_scale + challenge*aut_sal*days_from_EP_scale + lrgen_scale  + EP_Coa + nat_opp + (1|debate) + as.factor(year) + as.factor(country) +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AUT_sal_cycle_DR)


Immi_sal_cycle <- lmer(immi_dict_scale ~ challenge*immi_sal*days_from_nat_scale + challenge*immi_sal*days_from_EP_scale + lrgen_scale  + EP_Coa + nat_opp +de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Immi_sal_cycle)

Immi_sal_cycle_DR <- lmer(immi_dict_scale ~ challenge*immi_sal*days_from_nat_scale + challenge*immi_sal*days_from_EP_scale + lrgen_scale  + EP_Coa + nat_opp + (1|debate) + as.factor(year) + as.factor(country) +
                           (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Immi_sal_cycle_DR)

texreg(list(AUT_sal_cycle, AUT_sal_cycle_DR, Immi_sal_cycle, Immi_sal_cycle_DR),
       file = "Tables/issues_sal_cycle_260424.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "aut_sal" = "Austerity Salience",
                              "immi_sal" = "Immigration Salience",
                              "days_from_nat_scale" = "Proximity to next National Election",
                              "days_from_EP_scale" = "Proximity to next EP-Election",
                              "challenge:aut_sal" = "Challenger * Austerity Salience",
                              "challenge:immi_sal" = "Challenger * Immigration Salience",
                              "challenge:days_from_nat_scale" = "Challenger * Proximity to next National Election",
                              "challenge:days_from_EP_scale" = "Challenger  * Proximity to next EP-Election",
                              "challenge:aut_sal:days_from_nat_scale" = "Challenger * Austerity Salience * Proximity to next National Election",
                              "challenge:immi_sal:days_from_nat_scale" = "Challenger * Immigration Salience * Proximity to next National Election",
                              "challenge:aut_sal:days_from_EP_scale" = "Challenger * Austerity Salience * Proximity to next EP-Election",
                              "challenge:immi_sal:days_from_EP_scale" = "Challenger * Immigration Salience * Proximity to next EP-Election",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "No", "Yes", "No"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes", "Yes"),
                              "Debate Random Effects" = c("No", "Yes", "No", "Yes")),
       custom.model.names = c("Austerity", "Austerity", "Immigration", "Immigration"),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 3,
       caption = "The effect of changes in issue salience on issue emphasis conditional on electoral cycles")





